<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <div class="jsbos_search">
        <el-form @submit.native.prevent label-width="90px">
          <el-form-item label="抽奖名称">
            <el-input
              v-model="query.title"
              style="width: 100%"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="抽奖形式">
            <el-select
              style="width: 100%"
              v-model="query.drawType"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) in drawTypeOptions"
                :key="index"
                :label="item.fullName"
                :value="item.enCode"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search()"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh-right" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screen-full isContainer />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options jsbos-common-head-icon"
                :underline="false"
                @click="() => this.$refs.jsbosTableRef.showDrawer()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          ref="jsbosTableRef"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="title"
            label="抽奖名称"
            width="200"
            align="left"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <keep-alive>
                <el-link
                  :underline="false"
                  @click="goDetail(scope.row.id)"
                  type="primary"
                  >{{ scope.row.title }}</el-link
                >
              </keep-alive>
            </template></el-table-column
          >
          <el-table-column
            prop="drawType"
            label="抽奖形式"
            width="0"
            align="left"
          />
          <el-table-column
            prop="publish"
            label="发布状态"
            width="0"
            align="left"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.publish == '1'" class="online" />
              <span v-else class="offline" />
              &nbsp;
              <span v-if="scope.row.publish == '1'"> 已发布 </span>
              <span v-else> 待发布 </span>
            </template></el-table-column
          >
          <el-table-column
            prop="startTime"
            label="开始时间"
            width="0"
            align="left"
          />
          <el-table-column
            prop="endTime"
            label="结束时间"
            width="0"
            align="left"
          />
          <el-table-column
            prop="enableNumber"
            label="每人能抽"
            width="0"
            align="left"
          />
          <el-table-column
            prop="signUpNumber"
            label="报名人数"
            width="0"
            align="left"
          />

          <el-table-column
            prop="creatorUserId"
            label="创建人"
            width="0"
            align="left"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="133"
            align="left"
          />
          <el-table-column label="操作" fixed="right" width="240">
            <template slot-scope="scope">
              <el-button type="text" @click="goDetail(scope.row.id)"
                >详情
              </el-button>
              <el-button type="text" @click="goSignUpUser(scope.row.id)"
                >报名人员
              </el-button>
              <el-tooltip
                v-if="scope.row.publish == 2"
                class="item"
                effect="dark"
                content="请发布后再进行抽奖"
                placement="left"
              >
                <el-button disabled type="text" @click="goDraw(scope.row.id)"
                  >在线抽奖
                </el-button>
              </el-tooltip>

              <el-button v-else type="text" @click="goDraw(scope.row.id)"
                >在线抽奖
              </el-button>

              <el-dropdown>
                <span class="el-dropdown-link" style="color: #1890ff">
                  <el-button type="text">更多</el-button>
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu
                  slot="dropdown"
                  :popper-append-to-body="false"
                >
                  <el-dropdown-item v-if="scope.row.publish == 1">
                    <el-tooltip
                      class="item"
                      effect="dark"
                      content="请取消发布后再进行编辑"
                      placement="left"
                    >
                      <el-button
                        disabled
                        type="text"
                        @click="addOrUpdateHandle(scope.row.id)"
                        >编辑
                      </el-button>
                    </el-tooltip>
                  </el-dropdown-item>
                  <el-dropdown-item v-else>
                    <el-button
                      type="text"
                      @click="addOrUpdateHandle(scope.row.id)"
                      >编辑
                    </el-button></el-dropdown-item
                  >
                  <el-dropdown-item>
                    <el-button
                      type="text"
                      class="jsbos-table-delBtn"
                      @click="handleDel(scope.row.id)"
                      >删除
                    </el-button></el-dropdown-item
                  >
                  <el-dropdown-item v-if="scope.row.publish == 2">
                    <el-button
                      type="text"
                      @click="handlePulish(scope.row.id, 1)"
                      >发布
                    </el-button></el-dropdown-item
                  >
                  <el-dropdown-item v-else>
                    <el-button
                      type="text"
                      @click="handlePulish(scope.row.id, 2)"
                      >取消发布
                    </el-button></el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
  </div>
</template>

<script>
import request from "@/utils/request";
import JSBOSForm from "./Form";
import ExportBox from "./ExportBox";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import Detail from "./Detail";

export default {
  name: "CmsAwardActivity",
  components: { JSBOSForm, ExportBox, Detail },
  data() {
    return {
      showAll: false,
      detailVisible: false,
      query: {
        title: undefined,
        drawType: undefined,
        startTime: undefined,
        enableNumber: undefined,
        drawRequire: undefined,
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "title", label: "抽奖名称" },
        { prop: "drawType", label: "抽奖形式" },
        { prop: "startTime", label: "抽奖时间" },
        { prop: "enableNumber", label: "每人能抽" },
        { prop: "drawRequire", label: "抽奖活动要求" },
      ],
      drawTypeOptions: [],
      drawTypeProps: {
        label: "fullName",
        value: "enCode",
      },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getDrawTypeOptions();
  },
  methods: {
    getDrawTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "drawType",
        })
        .then((res) => {
          this.drawTypeOptions = res;
        });
    },
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id, 1);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/cms/CmsAwardActivity/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/cms/CmsAwardActivity/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/cms/CmsAwardActivity/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jsbos.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },

    //跳转到报名人员列表
    goSignUpUser(activityId) {
      this.$router.push({
        path: `/cms/user?activityId=${activityId}`,
      });
    },
    //打开新标签，跳转到抽奖页面
    goDraw(activityId) {
      let page = this.$router.resolve({
        path: `/cms/draw?applicationId=62634257855126848&activityId=${activityId}`,
      });
      window.open(page.href, "_blank");
    },
    //发布 取消发布
    handlePulish(data, status) {
      var _data = { id: data, publish: status };
      this.$confirm("此操作将更新发布状态, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: "/api/cms/CmsAwardActivity/publish/" + data,
            method: "PUT",
            data: _data,
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: "success",
              duration: 1000,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.online {
  width: 10px;
  height: 10px;
  display: inline-table;
  border-radius: 50%;
  background: var(--green);
  color: var(--green);
}

.offline {
  width: 10px;
  height: 10px;
  display: inline-table;
  border-radius: 50%;
  background: var(--orange);
  color: var(--orange);
}
</style>
